package org.tgcms.core.dal.appengine.dao;

import org.tgcms.core.dal.appengine.domain.AppenginePage;
import org.tgcms.core.dal.appengine.domain.AppengineWebsite;
import org.tgcms.core.dal.dao.DaoException;
import org.tgcms.core.dal.dao.PageDao;
import org.tgcms.core.dal.dto.Page;
import org.tgcms.core.dal.dto.Website;

import com.google.appengine.api.datastore.Key;


public class AppenginePageDao extends AppengineEntityDao<AppenginePage, Page, Website> implements PageDao {

	public AppenginePageDao() {
		super(AppenginePage.class, Page.class);
	}

	@Override
	protected AppenginePage asAppengineEntity(Page page)
			throws DaoException {
		if (page instanceof AppenginePage) {
			return (AppenginePage) page;
		}
		return new AppenginePage(page);
	}
	
	@Override
	public Page create(Website website) throws DaoException {
		AppengineWebsite appengineWebsite = checkWebsite(website);
		Key key = datastore.allocateIds(appengineWebsite.getKey(), AppenginePage.class.getSimpleName(), 1).iterator().next();
		return new AppenginePage(key);
	}

	
}
